﻿using System;
using System.Linq;
using System.Text;

namespace AutoDb {
	/// <summary>
	/// 
	/// </summary>
	static class NameHelper {
		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string ToPascal(string s) {
			if (string.IsNullOrEmpty(s)) {
				return string.Empty;
			}

			var sb = new StringBuilder(s.Length);

			var f = true;
			foreach (var c in s) {
				if (c == '_') {
					f = true;
					continue;
				}

				if (f) {
					sb.Append(char.ToUpper(c));
					f = false;
				} else {
					sb.Append(c);
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string ToCamel(string s) {
			if (string.IsNullOrEmpty(s)) {
				return string.Empty;
			}

			var sb = new StringBuilder(s.Length);

			var f = true;
			foreach (var c in s) {
				if (c == '_') {
					f = true;
					continue;
				}

				if (f) {
					sb.Append(char.ToUpper(c));
					f = false;
				} else {
					sb.Append(c);
				}
			}

			if (sb.Length > 0) {
				sb[0] = char.ToLower(sb[0]);
			}

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string ToUpper(string s) {
			if (string.IsNullOrEmpty(s)) {
				return string.Empty;
			}

			var sb = new StringBuilder(s.Length);

			foreach (var c in s) {
				if (c == '_') {
					continue;
				}

				sb.Append(char.ToUpper(c));
			}

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string ToLower(string s) {
			if (string.IsNullOrEmpty(s)) {
				return string.Empty;
			}

			var sb = new StringBuilder(s.Length);

			foreach (var c in s) {
				if (c == '_') {
					continue;
				}

				sb.Append(char.ToLower(c));
			}

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <param name="cnt"></param>
		/// <returns></returns>
		public static string Convert(string s, Func<string, string> cnt) {
			System.Diagnostics.Debug.Assert(s != null);

			if (s.Length == 0) {
				return string.Empty;
			}

			return string.Join(".", s.Split('.').Select(w => w.Length == 0 ? "" : cnt(w)));
		}
	} // end of NameHelper.
}
